package sample1;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.strx.Parser;
import org.strx.Status;
import org.strx.TagHandler;

public class Simple1 {
	public static void main(String[] args) throws IOException {
		InputStream is = Simple1.class.getResourceAsStream("data.xml");
		byte[] buff = new byte[is.available()];
		is.read(buff);
		String xml = new String(buff, "UTF-8");
		TagHandler<List<String>> handler = new SimpleHandler();
		Parser parser = new Parser(xml, handler);
		parser.parse();
		System.out.println(handler.getResult());
	}
}

class SimpleHandler extends TagHandler<List<String>> {
	private List<String> list;

	@Override
	protected void onStart(Status status) {
		String path = status.getPath();
		if ("root.element".equals(path)) {
			list = new ArrayList<String>();
		} else if ("root.element.item".equals(path)) {
			String name = status.getAttribute("name");
			list.add(name);
		}
	}

	@Override
	protected void onEnd(Status status) {
		String path = status.getPath();
		if ("root.element".equals(path)) {
			setResult(list);
			terminate();
		}
	}
}
